import {QueryTypes} from "sequelize";
import {sequelize} from "../../config/mysqlDB.js";
import jwt from "jsonwebtoken";
import {Result} from "../../utils/result.js";
import constant from "../../common/constant.js";


const loginService = {
  async login(username, password) {
    let res = await sequelize.query(`select * from user where username = '${username}' and password = '${password}'`,
      {
        type: QueryTypes.SELECT,
      });
    if (res.length > 0) {
      let token = jwt.sign({username}, constant.JWT_SECRET, {expiresIn: constant.JWT_EXPIRES_IN}, null);
      return Result.success(token);
    }
    return Result.success({}, '用户名或密码错误');
  },


}
export default loginService;
